package iso3.pt.action;

import java.util.Map;

import iso3.pt.model.Alumno;
import iso3.pt.model.Profesor;
import iso3.pt.service.PtDaoService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;

public class LoginAction extends ActionSupport implements Preparable
{
	private static final long serialVersionUID = 1L;
	private int username = 0 ;
    private String password = null;
    private String type ;
    
//get y sets-------------------------------------------------------
    
    public int getUsername() 
	{
		return username;
	}

	public String getType()
	{
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public void setUsername(int username) 
	{
		this.username = username;
	}

	public String getPassword() 
	{
		return password;
	}

	public void setPassword(String password) 
	{
		this.password = password;
	}
	
//FIN get y sets-------------------------------------------------------
	
	@SuppressWarnings("unchecked")
	public String login()
	{
		PtDaoService hibernateDAO = new PtDaoService();
		@SuppressWarnings("rawtypes")
		Map session = ActionContext.getContext().getSession(); //para el login
		
		String resultado = "";
		
		if ( this.password.equals("") || this.username == 0)
		{
			resultado = INPUT;
			addActionError(getText("error.login.missingDetails"));
		}
		else if(this.type.equals(getText("label.login.student"))) 	//login student
		{
			try
			{
				Alumno al = hibernateDAO.loginAlumno(this.username,this.password);
				resultado = "okStudent";
				
			    session.put("logged-in", al);
				
			}catch(Exception ex )
			{
				resultado = ERROR;
				addActionError(getText("error.login.invalidDetails"));
			}
		}
		else													//login professor
		{
			try
			{
				Profesor pr = hibernateDAO.loginProfesor(this.username,this.password);
				resultado = "okProfessor";
				
				session.put("logged-in",pr);
				
			}catch(Exception ex )
			{
				resultado=ERROR;
				addActionError(getText("error.login.invalidDetails"));
		
			}
		}
		
		return resultado;
		
	}
	
	@Override
	public void prepare() throws Exception //void method
	{}
}
